﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace CardPlay.Model.Primitives
{
    [StructLayout(LayoutKind.Sequential)]
    [DebuggerDisplay("Width={Width}, Height={Height}")]
    public struct Size
    {
        private int _width;
        private int _height;

        public static readonly Size Empty;

        static Size()
        {
            Empty = new Size();
        }

        public Size(Point pt)
        {
            _width = pt.X;
            _height = pt.Y;
        }

        public Size(int width, int height)
        {
            _width = width;
            _height = height;
        }

        public static Size operator +(Size sz1, Size sz2)
        {
            return Add(sz1, sz2);
        }

        public static Size operator -(Size sz1, Size sz2)
        {
            return Subtract(sz1, sz2);
        }

        public static bool operator ==(Size sz1, Size sz2)
        {
            return ((sz1.Width == sz2.Width) && (sz1.Height == sz2.Height));
        }

        public static bool operator !=(Size sz1, Size sz2)
        {
            return !(sz1 == sz2);
        }

        public static explicit operator Point(Size size)
        {
            return new Point(size.Width, size.Height);
        }

#if !WINDOWS_PHONE
        [Browsable(false)]
#endif
        public bool IsEmpty
        {
            get { return (_width == 0) && (_height == 0); }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public static Size Add(Size first, Size second)
        {
            return new Size(first.Width + second.Width, first.Height + second.Height);
        }

        public static Size Subtract(Size first, Size second)
        {
            return new Size(first.Width - second.Width, first.Height - second.Height);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is Size))
            {
                return false;
            }
            var size = (Size)obj;
            return ((size._width == _width) && (size._height == _height));
        }

        public override int GetHashCode()
        {
            return (_width ^ _height);
        }

        public override string ToString()
        {
            return String.Format("{{Width={0}, Height={1}}}", Width, Height);
        }
    }
}

